﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.Linq;

namespace DevEx
{
    class CSanPham
    {
        private static QLVTDataContext GetDc()
        {
            QLVTDataContext dc = new QLVTDataContext();
            return dc;
        }

        public IQueryable<tblSanPham> SelectAllRecord()
        {
            QLVTDataContext dc = GetDc();

            var q = from p in dc.tblSanPhams
                    select p;
            return q;
        }

        public tblSanPham SelectRecordByID(int ID_SanPham)
        {
            QLVTDataContext dc = GetDc();

            var q = (from p in dc.tblSanPhams where p.ID_SanPham == ID_SanPham select p).SingleOrDefault();
            return q;
        }

        public tblSanPham SelectRecordByName(string SanPham)
        {
            QLVTDataContext dc = GetDc();

            var q = (from p in dc.tblSanPhams where p.SanPham == SanPham select p).FirstOrDefault();
            return q;
        }

        public IQueryable<tblSanPham> SelectSanPhamByLoaiSanPham(int ID_Parent)
        {
            QLVTDataContext dc = GetDc();

            var q = from p in dc.tblSanPhams where p.ID_Parent == ID_Parent select p;
            return q;
        }

        //Insert
        public void Insert(tblSanPham obj)
        {
            QLVTDataContext dc = GetDc();
            dc.tblSanPhams.InsertOnSubmit(obj);
            dc.SubmitChanges();
        }
        //Delete
        public void Delete(int ID)
        {
            QLVTDataContext dc = GetDc();

            var kho = dc.tblSanPhams.SingleOrDefault(p => p.ID_SanPham == ID);
            dc.tblSanPhams.DeleteOnSubmit(kho);
            dc.SubmitChanges();
        }
        //Update
        public void Update(tblSanPham obj)
        {
            QLVTDataContext dc = GetDc();
            dc.tblSanPhams.Attach(obj);
            dc.Refresh(RefreshMode.KeepCurrentValues, obj);
            dc.SubmitChanges(ConflictMode.ContinueOnConflict);
        }
    }
}
